package jt.classic.app.dbtx;

import java.sql.Connection;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import jt.classic.system.SimpleManager;

import org.limp.basework.SimpleBean;
import org.limp.basework.impl.CommonBean4HSQ;
import org.limp.basework.impl.SimpleBeanImpl;
import org.limp.basework.tools.BaseworkUtil;
import org.limp.mine.DateTrimmer;
import org.limp.mine.StringTool;

public class DbtxManager extends SimpleManager {
    public static Map DBTYPE = new Hashtable();

    static {
        DBTYPE.put("gwbl", "公文办理");
        DBTYPE.put("xzxk", "行政许可");
        DBTYPE.put("yjsj", "应急事件");
        DBTYPE.put("shfb", "审核发布");
        DBTYPE.put("hytx", "会议提醒");
        DBTYPE.put("normal", "个人日程");

        /*
        DBTYPE.put("xxgx", "信息更新");*/
    }

    public DbtxManager(Connection conn) {
        super(conn);
    }

    /**
     * 获得待办类型名称
     *
     * @param type 待办类型
     * @return String
     */
    public static String getTypeName(String type) {
        return (String) DBTYPE.get(type);
    }

    /**
     * 根据待办类型取到待办提醒
     *
     * @param type 待办类型
     * @param recordNum
     *            每次取的记录数
     * @return 记录集
     */
    public List getDbtxByType(String type, int recordNum,String userId) {
        String sql = "SELECT * FROM	app_dbtx where TYPE = '" + type + "' and creatorid = '" + userId + "' order by CREATETIME desc";

        List list = this.trimmer.searchMultiData(sql, recordNum);

        return list;
    }

    /**
     * 获得待办信息
     * @param id
     * @return
     */
    public Map getDbtxById(String id) {
        String sql = "SELECT * FROM	app_dbtx where ID = '" + id + "' ";

        return this.trimmer.searchSingleData(sql);
    }

    /**
     * 获得待办属性信息
     * @param id
     * @return
     */
    public Map getDbtxPropById(String id) {
        String sql = "SELECT * FROM app_dbtx_prop WHERE ID = '" + id + "'";

        Map info = this.trimmer.searchSymmetricalData(sql, "PNAME", "PVALUE");

        return info;
    }

    /**
     * 插入普通待办提醒
     *
     * @param request
     */
    public void insert(HttpServletRequest request) throws Exception {
        CommonBean4HSQ cbh = new CommonBean4HSQ(request);

        Map param = cbh.getResource();

        /*待办信息提醒时间*/
        String remindtime = StringTool.checkString(param.get("remindtime"));

        SimpleBean sb = BaseworkUtil.analyzerSimpleBean("APP_DBTX", cbh);

        /*待办信息创建时间*/
        sb.getResource().put("createtime", DateTrimmer.getYMDHMS());

        /*设置待办提醒时间*/
        if (remindtime.equals("")) {
            sb.getResource().put("remindtime", DateTrimmer.getYMDHMS());
        }

        BaseworkUtil util = new BaseworkUtil();

        Map info = util.manualSave("APP_DBTX", sb, conn);

        String id = (String) info.get("ID");

        String propCount = StringTool.checkString(param.get("propCount"), "0");

        int _propCount = Integer.parseInt(propCount);

        if (_propCount > 0) {
            for (int i = 0; i < _propCount; i++) {
                String propKey = (String) param.get("propKey_" + i);
                String propValue = (String) param.get("propValue_" + i);

                SimpleBean _sb = new SimpleBeanImpl();

                _sb.getResource().put("id", id);
                _sb.getResource().put("pname", propKey);
                _sb.getResource().put("pvalue", propValue);

                util.manualSave("APP_DBTX_PROP", _sb, conn);
            }
        }
    }
    
    
    /**
     * 插入普通待办提醒
     *
     * @param request
     */
    public void insert(Map param) throws Exception {
        CommonBean4HSQ cbh = new CommonBean4HSQ(null);
        cbh.getResource().putAll(param);

        /*待办信息提醒时间*/
        String remindtime = StringTool.checkString(param.get("remindtime"));

        SimpleBean sb = BaseworkUtil.analyzerSimpleBean("APP_DBTX", cbh);

        /*待办信息创建时间*/
        sb.getResource().put("createtime", DateTrimmer.getYMDHMS());

        /*设置待办提醒时间*/
        if (remindtime.equals("")) {
            sb.getResource().put("remindtime", DateTrimmer.getYMDHMS());
        }

        BaseworkUtil util = new BaseworkUtil();

        Map info = util.manualSave("APP_DBTX", sb, conn);

        String id = (String) info.get("ID");

        String propCount = StringTool.checkString(param.get("propCount"), "0");

        int _propCount = Integer.parseInt(propCount);

        if (_propCount > 0) {
            for (int i = 0; i < _propCount; i++) {
                String propKey = (String) param.get("propKey_" + i);
                String propValue = (String) param.get("propValue_" + i);

                SimpleBean _sb = new SimpleBeanImpl();

                _sb.getResource().put("id", id);
                _sb.getResource().put("pname", propKey);
                _sb.getResource().put("pvalue", propValue);

                util.manualSave("APP_DBTX_PROP", _sb, conn);
            }
        }
    }
}
